package com.xkstream.client.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.base.BaseRecycleViewAdapter;
import com.xkstream.client.config.LevelConfig;
import com.xkstream.client.lgim.LgExtension;
import com.xkstream.client.lgim.LgMessage;
import com.xkstream.client.lgim.LgMessageParser;

import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017-10-11.
 */

public class ChatRoomAdapter_Lg extends BaseRecycleViewAdapter {
    private List<Object> mDatas;

    public ChatRoomAdapter_Lg(Context mContext, List<Object> mDatas) {
        super(mContext);
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_chatitem, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindItemHolder((ItemHolder) holder, position);
    }

    private void onBindItemHolder(ItemHolder holder, int pos) {
        Object item = mDatas.get(pos);
        if (item instanceof String) {
            holder.tvContent.setText(item.toString());
            holder.tvContent.setOnClickListener(null);
            holder.tvLevel.setVisibility(View.GONE);
        } else {
            bindMessage(holder, (LgMessage) mDatas.get(pos));
        }
    }

    private void bindMessage(ItemHolder holder, LgMessage message) {
        switch (message.getCmmid()) {
            case LgMessageParser.CODE_NORMAL:
                bindNormalMessage(holder, message);
                break;
            case LgMessageParser.CODE_GIFT:
                bindGiftMessage(holder, message);
                break;
            case LgMessageParser.CODE_ENTER:
                bindEnterMessage(holder, message);
                break;
        }
    }

    private void bindGiftMessage(ItemHolder holder, LgMessage message) {
        holder.tvLevel.setVisibility(View.VISIBLE);
        int exp = message.getData().getExtInfo().getExperience();
        int level = LevelConfig.getLevel(exp);
        int bgRes = LevelConfig.getLevelBgRes(level);
        holder.tvLevel.setText("" + level);
        holder.tvLevel.setBackgroundResource(bgRes);
        String nick = message.getData().getGiftInfo().getSenderName();
        String gift = message.getData().getGiftInfo().getGift_name();
        holder.tvContent.setText(Html.fromHtml(
                String.format(holder.chatItemGift, nick, gift + "")
        ));
    }

    private void bindEnterMessage(ItemHolder holder, LgMessage message) {
        String nickname = message.getData().getExtInfo().getNickname();
        holder.tvLevel.setVisibility(View.GONE);
        holder.tvContent.setText(Html.fromHtml(String.format(holder.chatItemEnter, nickname)
                , source -> {
                    int id = mContext.getResources().getIdentifier(source, "drawable", mContext.getPackageName());
                    Drawable drawable = mContext.getResources().getDrawable(id);
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                    return drawable;
                }, null));

    }

    private void bindNormalMessage(ItemHolder holder, LgMessage message) {
        holder.tvLevel.setVisibility(View.VISIBLE);
        int exp = message.getData().getExtInfo().getExperience();
        int level = LevelConfig.getLevel(exp);
        int bgRes = LevelConfig.getLevelBgRes(level);
        holder.tvLevel.setText("" + level);
        holder.tvLevel.setBackgroundResource(bgRes);
        String nickname = message.getData().getExtInfo().getNickname();
        String content = message.getData().getContent();
        holder.tvContent.setText(Html.fromHtml(String.format(
                holder.chatItemNormal, nickname, content
        )));
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindString(R.string.format_chatitem_normal)
        String chatItemNormal;
        @BindString(R.string.format_chatitem_gift)
        String chatItemGift;
        @BindString(R.string.format_chatitem_enter)
        String chatItemEnter;

        @BindView(R.id.tvContent)
        TextView tvContent;
        @BindView(R.id.tvLevel)
        TextView tvLevel;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
